<?php

namespace console\controllers;

use Yii;
use services\models\Product;
/**
 * 定时更新首页每日一试，限时抢购
 */
class IndexManageController extends \yii\console\Controller {
    public function actionIndex() {
        $datetime = date('Y-m-d 00:00:00');
        $connection = Yii::$app->db;
        
        //====================================================每日一试BEGIN============================================
        $sql = "select * from day_try where status=1 order by recommend_time desc";     
        $result = $connection->createCommand($sql)->queryAll();
        if(!empty($result))
        {
            $flag = 0;
            foreach($result as $k=>$v)
            {
                if($v['recommend_time'] >= $datetime)
                {
                    $index_manage['day_try'][$k]['product_id'] = $v['product_id'];
                    $index_manage['day_try'][$k]['pic'] = $v['pic'];
                    $index_manage['day_try'][$k]['recommend_time'] = $v['recommend_time'];
                }elseif($flag != 1)
                {
                    $index_manage['day_try'][$k]['product_id'] = $v['product_id'];
                    $index_manage['day_try'][$k]['pic'] = $v['pic'];
                    $index_manage['day_try'][$k]['recommend_time'] = $v['recommend_time'];
                    $flag = 1;
                }
            }
        }
        //======================================================每日一试END================================================
        
        //======================================================超值试用BEGIN================================================
        $sql = "select * from super_try order by position,recommend_time desc";     
        $result = $connection->createCommand($sql)->queryAll();
        if(!empty($result))
        {
            $flag1 = $flag2= $flag3 = 0;
            foreach($result as $k=>$v)
            {
                $product_info = Product::findOne(['product_id'=>$v['product']]);
                if($v['recommend_time'] >= $datetime)
                {
                     $index_manage['super_try'][$v['position']][$k]['product_id'] = $v['product'];
                     $index_manage['super_try'][$v['position']][$k]['goods_id'] = $product_info->goods_id;
                     $index_manage['super_try'][$v['position']][$k]['recommend_time'] = $v['recommend_time'];
                 }elseif(($v['position'] == 1 && $flag1 != 1) || ($v['position'] == 2 && $flag2 != 1) || ($v['position'] == 3 && $flag3 != 1))
                 {
                     $index_manage['super_try'][$v['position']][$k]['product_id'] = $v['product'];
                     $index_manage['super_try'][$v['position']][$k]['goods_id'] = $product_info->goods_id;
                     $index_manage['super_try'][$v['position']][$k]['recommend_time'] = $v['recommend_time'];
                     $flag1 = $v['position'] == 1 ? 1 : 0;
                     $flag2 = $v['position'] == 2 ? 1 : 0;
                     $flag3 = $v['position'] == 3 ? 1 : 0;
                 }
            }
        }
        //========================================================超值试用END================================================

        //========================================================小试推荐BEGIN================================================
        $sql = "SELECT * FROM `try_rec` where `status`=1 ORDER BY display_position,recommend_time desc;";     
        $result = $connection->createCommand($sql)->queryAll();
        if(!empty($result))
        {
            $flag1 = $flag2 = $flag3 = $flag4 = 0;
            foreach($result as $k=>$v)
            {
                if($v['recommend_time'] >= $datetime)
                {
                    $index_manage['try_rec'][$v['display_position']][$k]['product_id1'] = $v['product1'];
                    $index_manage['try_rec'][$v['display_position']][$k]['product_id2'] = $v['product2'];
                    $index_manage['try_rec'][$v['display_position']][$k]['product_id3'] = $v['product3'];
                    $index_manage['try_rec'][$v['display_position']][$k]['pic'] = $v['pic1'];
                    $index_manage['try_rec'][$v['display_position']][$k]['recommend_time'] = $v['recommend_time'];
                }elseif(($v['display_position'] == 1 && $flag1 != 1) || ($v['display_position'] == 2 && $flag2 != 1) || ($v['display_position'] == 3 && $flag3 != 1) || ($v['display_position'] == 4 && $flag4 != 1))
                {
                    $index_manage['try_rec'][$v['display_position']][$k]['product_id1'] = $v['product1'];
                    $index_manage['try_rec'][$v['display_position']][$k]['product_id2'] = $v['product2'];
                    $index_manage['try_rec'][$v['display_position']][$k]['product_id3'] = $v['product3'];
                    $index_manage['try_rec'][$v['display_position']][$k]['pic'] = $v['pic1'];
                    $index_manage['try_rec'][$v['display_position']][$k]['recommend_time'] = $v['recommend_time'];
                    $flag1 = $v['display_position'] == 1 ? 1 : 0;
                    $flag2 = $v['display_position'] == 2 ? 1 : 0;
                    $flag3 = $v['display_position'] == 3 ? 1 : 0;
                    $flag4 = $v['display_position'] == 4 ? 1 : 0;
                }
            }
        }
        //========================================================小试推荐END================================================
        
        //========================================================LOGO右侧广告BEGIN================================================
        $sql = "SELECT * FROM `logo_adv`  where `status`=1 and '$datetime' between recommend_starttime and recommend_endtime ORDER BY add_time desc limit 1";     
        $result = $connection->createCommand($sql)->queryAll();
        
        if(!empty($result))
        {
            foreach($result as $v)
            {
                $index_manage['logo_adv'][] = $v;
            }
        }
        //========================================================LOGO右侧广告END================================================
        
        //========================================================大试特试BEGIN================================================
        $sql = "SELECT * FROM `try_adv` where `status`=1 ORDER BY type,recommend_time desc;";     
        $result = $connection->createCommand($sql)->queryAll();
        if(!empty($result))
        {
            $flag1 = $flag2= $flag3 = 0;
            foreach($result as $k=>$v)
            {
                if($v['recommend_time'] >= $datetime)
                {
                     $index_manage['try_adv'][$v['type']][$k]['pic'] = $v['pic'];
                     $index_manage['try_adv'][$v['type']][$k]['url'] = $v['url'];
                     $index_manage['try_adv'][$v['type']][$k]['recommend_time'] = $v['recommend_time'];
                 }elseif(($v['type'] == 1 && $flag1 != 1) || ($v['type'] == 2 && $flag2 != 1) || ($v['type'] == 3 && $flag3 != 1))
                 {
                     $index_manage['try_adv'][$v['type']][$k]['pic'] = $v['pic'];
                     $index_manage['try_adv'][$v['type']][$k]['url'] = $v['url'];
                     $index_manage['try_adv'][$v['type']][$k]['recommend_time'] = $v['recommend_time'];
                     $flag1 = $v['type'] == 1 ? 1 : 0;
                     $flag2 = $v['type'] == 2 ? 1 : 0;
                     $flag3 = $v['type'] == 3 ? 1 : 0;
                 }
            }
        }
        //========================================================大试特试END================================================
    }
}